package com.abel.bigwater.data.sanchuan

import com.abel.bigwater.data.BcdString
import io.netty.buffer.Unpooled
import io.netty.channel.embedded.EmbeddedChannel
import org.joda.time.DateTime
import org.junit.Test

class WeekDetailDecoderTests {
    @Test
    fun testDecodeMonthBill() {
        val mbill = FetchDataReplyMonthBill(null).apply {
            this.cmd = FetchCmdType.MonthBill
            this.startDate = DateTime(2018, 2, 1, 0, 0)
            this.decimalBit = 1
            this.dataList = arrayListOf(123.4, 567.8, 901.2, 345.6, 789.0, 123.4)
        }

        val pkt = SanchuanPacket().apply {
            this.ctrlb = CtrlbType.FetchDetailReply
            this.actrlb = 2
            this.meterId = BcdString(byteArrayOf(10, 11, 2, 3, 4, 5, 36, 1), true)
            data = mbill
        }

        val buf = Unpooled.buffer().apply {
            pkt.fillBuffer(this)
        }

        val ch = EmbeddedChannel(SanchuanDecoder()).also {
            it.writeInbound(buf)
        }

        Thread.sleep(1000)
    }

    @Test
    fun testDayBill() {
        val dbill = FetchDataReplyDayBill(null).apply {
            this.cmd = FetchCmdType.DayBill
            this.startDate = DateTime(2018, 8, 1, 0, 0)
            this.decimalBit = 2
            this.dataList = arrayListOf(12.34, 56.78, 90.12)
        }

        val pkt = SanchuanPacket().apply {
            this.ctrlb = CtrlbType.FetchDetailReply
            this.actrlb = 2
            this.meterId = BcdString(byteArrayOf(10, 11, 2, 3, 4, 5, 36, 1), true)
            data = dbill
        }

        val buf = Unpooled.buffer().also { pkt.fillBuffer(it) }

        // channel
        val ch = EmbeddedChannel(SanchuanDecoder()).also { it.writeInbound(buf) }

        Thread.sleep(1000)
    }

    @Test
    fun testWeekDetail() {
        val wd = FetchDataReplyWeekDetail(null).apply {
            this.cmd = FetchCmdWeekDetail(0).also {
                it.periodType = FetchCmdWeekDetail.DetailPeriodType.TenMinutes
                it.dataType = FetchCmdWeekDetail.DetailDataType.TotalForward
            }

            this.startDate = DateTime(2018, 8, 2, 6, 0)
            this.timeInterval = 1
            this.pointNumber = 6
            this.totalForwardBit = 2
            this.forwardLongList = arrayListOf(12, 340, 560, 780, 900, 1002)
        }

        val pkt = SanchuanPacket().apply {
            this.ctrlb = CtrlbType.FetchDetailReply
            this.actrlb = 2
            this.meterId = BcdString(byteArrayOf(10, 11, 2, 3, 4, 5, 36, 1), true)
            this.data = wd
        }

        val buf = Unpooled.buffer().also { pkt.fillBuffer(it) }

        // channel
        val ch = EmbeddedChannel(SanchuanDecoder()).also { it.writeInbound(buf) }
        Thread.sleep(1000)
    }

    @Test
    fun testWeekDetailPressure() {
        val wd = FetchDataReplyWeekDetail(null).apply {
            this.cmd = FetchCmdWeekDetail(0).also {
                it.periodType = FetchCmdWeekDetail.DetailPeriodType.TenMinutes
                it.dataType = FetchCmdWeekDetail.DetailDataType.Pressure
            }

            this.startDate = DateTime(2018, 8, 2, 6, 0)
            this.timeInterval = 1
            this.pointNumber = 6
            this.totalForwardBit = 2
            this.forwardLongList = arrayListOf(12, 340, 560, 780, 900, 1002)

            this.pressureBit = 0
            this.pressureIntList = arrayListOf(30, 31, 32, 33, 32, 31)
        }

        val pkt = SanchuanPacket().apply {
            this.ctrlb = CtrlbType.FetchDetailReply
            this.actrlb = 2
            this.meterId = BcdString(byteArrayOf(10, 11, 2, 3, 4, 5, 36, 1), true)
            this.data = wd
        }

        val buf = Unpooled.buffer().also { pkt.fillBuffer(it) }

        // channel
        val ch = EmbeddedChannel(SanchuanDecoder()).also { it.writeInbound(buf) }
        Thread.sleep(1000)
    }

    @Test
    fun testWeekDetailAll() {
        val wd = FetchDataReplyWeekDetail(null).apply {
            this.cmd = FetchCmdWeekDetail(0).also {
                it.periodType = FetchCmdWeekDetail.DetailPeriodType.TenMinutes
                it.dataType = FetchCmdWeekDetail.DetailDataType.AllFour
            }

            this.startDate = DateTime(2018, 8, 2, 6, 0)
            this.timeInterval = 1
            this.pointNumber = 6
            this.totalForwardBit = 2
            this.forwardLongList = arrayListOf(12, 340, 560, 780, 900, 1002)

            this.pressureBit = 0
            this.pressureIntList = arrayListOf(30, 31, 32, 33, 32, 31)

            this.revertBit = 2
            this.revertList = arrayListOf(BcdString(byteArrayOf(1, 2, 3, 4, 5), false),
                    BcdString(byteArrayOf(1, 2, 3, 4, 5), false),
                    BcdString(byteArrayOf(1, 2, 3, 4, 5), false),
                    BcdString(byteArrayOf(1, 2, 3, 4, 5), false),
                    BcdString(byteArrayOf(1, 2, 3, 4, 5), false),
                    BcdString(byteArrayOf(1, 2, 3, 4, 5), false))

            this.flowBit = 2
            this.flowList = arrayListOf(2, 3, 4, 5, 6, 7)
        }

        val pkt = SanchuanPacket().apply {
            this.ctrlb = CtrlbType.FetchDetailReply
            this.actrlb = 2
            this.meterId = BcdString(byteArrayOf(10, 11, 2, 3, 4, 5, 36, 1), true)
            this.data = wd
        }

        val buf = Unpooled.buffer().also { pkt.fillBuffer(it) }

        // channel
        val ch = EmbeddedChannel(SanchuanDecoder()).also { it.writeInbound(buf) }
        Thread.sleep(1000)
    }
}